package com.jh.controller;

import com.jh.constant.JwtClaimsConstant;
import com.jh.dto.*;
import com.jh.entity.Employee;
import com.jh.properties.JwtProperties;
import com.jh.result.PageResult;
import com.jh.result.Result;
import com.jh.service.EmployeeService;
import com.jh.utils.JwtUtil;
import com.jh.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private  JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        try {
            log.info("员工登录：{}", employeeLoginDTO);

            Employee employee = employeeService.login(employeeLoginDTO);

            // 登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);

            EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                    .id(employee.getId())
                    .phone(employee.getPhone())
                    .name(employee.getName())
                    .token(token)
                    .position(String.valueOf(employee.getPosition()))
                    .build();

            return Result.success(employeeLoginVO);
        } catch (Exception e) {
            log.error("员工登录失败：{}", employeeLoginDTO, e);
            return Result.error("账号或者密码错误");
        }

    }

    @PostMapping("/updatePassword")
    @ApiOperation("员工密码修改")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("员工密码修改，employeeDTO：{}",passwordEditDTO);
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();
    }

    @GetMapping("/getEmployeeData")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询,employeePageQueryDTO：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.page(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/editEmployee")
    @ApiOperation("员工修改")
    public Result edit(@RequestBody EmployeeDTO employeeDTO){
        log.info("员工修改，employeeDTO：{}",employeeDTO);
        employeeService.edit(employeeDTO);
        return Result.success("修改成功");
    }

    @GetMapping("/deleteEmployee")
    @ApiOperation("员工删除")
    public  Result delete(Long id){
        log.info("员工删除，id:{}",id);
        employeeService.delete(id);
        return Result.success();
    }

    @PostMapping("/addEmployee")
    @ApiOperation("员工新增")
    public Result add(@RequestBody EmployeeDTO employeeDTO){
        log.info("员工新增，employeeDTO：{}",employeeDTO);
        String date=employeeService.save(employeeDTO);
        return Result.success(date);
    }

}
